package com.learn._0fundamental;

/**
 * 不同的计算多项式的方法带来的时间上的差异
 */
public class Polynomial {


    public static void main(String[] args) {

        int times = 1;
        int n = 10000000;
        int x = 1;
        int c = 1;

        int[] input = new int[n];
        for(int i = 0;i < input.length;i ++){
            input[i] = c;
        }

        long start = System.currentTimeMillis();
        while(times -- > 0){
            //System.out.println(calculate(input,x));
            System.out.println(calculateReverse(input,x));
        }
        long end = System.currentTimeMillis();

        System.out.println("共用时"+(end - start));
    }


    public static long calculate(int[] nums,int x){

        int length = nums.length - 1;
        int tmp = 0;
        long res = 0;
        while(tmp <= length){
            res += nums[tmp] * Math.pow(x,tmp);
            tmp ++;
        }
        return res;
    }

    public static long calculateReverse(int[] nums,int x){

        int length = nums.length - 1;
        long res = nums[length] * x + nums[length - 1];
        length --;
        while(length > 0){
            res = res * x + nums[length - 1];
            length --;
        }
        return res;
    }






}
